package com.xana.acg.mikomiko.actis.anime;

import android.view.Menu;
import android.view.MenuItem;

import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.xana.acg.com.app.ToolbarActivity;
import com.xana.acg.com.data.DataSource;
import com.xana.acg.com.view.recycler.RecyclerV;
import com.xana.acg.fac.model.biliAnime.AnimeResp;
import com.xana.acg.fac.net.NetCallBack;
import com.xana.acg.fac.net.Network;
import com.xana.acg.mikomiko.R;
import com.xana.acg.mikomiko.frags.anime.AnimeFragment;
import com.xana.acg.mikomiko.frags.anime.AnimeFilterFragment;
import com.xana.acg.mikomiko.listener.SelfDrawerListener;

import butterknife.BindView;
import retrofit2.Call;

public class AnimeIndexActivity extends ToolbarActivity
        implements DataSource.Callback<AnimeResp>,
        AnimeFilterFragment.Callback,
        RecyclerV.OnMoreLoadListener {

    @BindView(R.id.mainDrawerLayout)
    DrawerLayout drawerLayout;

    @BindView(R.id.recycler)
    RecyclerV recyclerV;

    private int page;
    Adapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_anime_index;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        drawerLayout.addDrawerListener(new SelfDrawerListener());
        VirtualLayoutManager lm = new VirtualLayoutManager(this);
        recyclerV.setLayoutManager(lm);
        recyclerV.setListener(this);
        DelegateAdapter adapter = new DelegateAdapter(lm);
        adapter.addAdapter(mAdapter = new Adapter());
        recyclerV.setAdapter(adapter);
    }
    String[] p ={
            "-1",
            "-1",
            "-1",
            "-1",
            "-1",
            "-1",
            "0",
            "0",
    };

    @Override
    protected void initData() {
        super.initData();
        request(page=1, false);
    }

    boolean refresh = false;

    private void request(int page, boolean refresh) {
        Call<AnimeResp> call = Network.biliAPI().getAnimes(
                p[0],
                p[1],
                p[2],
                p[3],
                p[4],
                p[5],
                p[6],
                p[7],
                page,
                20
        );
        call.enqueue(new NetCallBack<>(this, refresh));
    }

    @Override
    public void ok(AnimeResp resp) {
        if(resp.refresh) {
            mAdapter.clear();
            recyclerV.scrollToPosition(0);
        }
        mAdapter.add(resp.data.list);
    }

    @Override
    public void fail(String msg) {
        showToast(msg);
    }

    @Override
    public void handle(int keyIndex, String value) {
        p[keyIndex] = value;
        request(page=1, true);
    }

    @Override
    public void onMoreLoad() {
        request(++page, false);
    }

    static class Adapter extends AnimeFragment.IndexAdapter{
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.filter, menu);
        MenuItem item = menu.findItem(R.id.filter);
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                drawerLayout.openDrawer(GravityCompat.END);
                return true;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onBackPressed() {
        if(drawerLayout.isDrawerOpen(GravityCompat.END)){
            drawerLayout.closeDrawer(GravityCompat.END);
            return;
        }
        super.onBackPressed();
    }
}

